package com.rao.crm.web.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.rao.crm.domain.PageBean;
import com.rao.crm.domain.SaleVisit;
import com.rao.crm.service.SaleVisitService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author Srao
 * @version 1.0
 * @className SaleVisitAction
 * @date 2019/9/21 20:02
 * @package com.rao.crm.web.action
 * @description
 */
public class SaleVisitAction extends ActionSupport implements ModelDriven<SaleVisit> {
    private SaleVisit saleVisit = new SaleVisit();
    @Resource(name = "saleVisitService")
    private SaleVisitService saleVisitService;
    private Integer currPage = 1;
    private Integer pageSize = 3;
    private Date visit_begin_time;
    private Date visit_end_time;

    public void setVisit_begin_time(Date visit_begin_time) {
        this.visit_begin_time = visit_begin_time;
    }

    public void setVisit_end_time(Date visit_end_time) {
        this.visit_end_time = visit_end_time;
    }

    @Override
    public SaleVisit getModel() {
        return saleVisit;
    }

    public void setCurrPage(Integer currPage) {
        if (currPage == null){
            currPage = 1;
        }
        this.currPage = currPage;
    }

    public void setPageSize(Integer pageSize) {
        if (pageSize == null){
            pageSize = 3;
        }
        this.pageSize = pageSize;
    }

    public String findAll(){
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SaleVisit.class);
        /*设置离线查询的条件*/
        if (visit_begin_time != null){
            detachedCriteria.add(Restrictions.ge("visit_time", visit_begin_time));
        }
        if (visit_end_time != null){
            detachedCriteria.add(Restrictions.le("visit_time", visit_end_time));
        }
        PageBean<SaleVisit> pageBean = saleVisitService.findAll(detachedCriteria, currPage, pageSize);
        ActionContext.getContext().getValueStack().push(pageBean);
        return "findAll";
    }

    public String saveUI(){

        return "saveUI";
    }



    public String save(){
        saleVisitService.save(saleVisit);
        return "save";
    }


    public String delete(){
        saleVisitService.delete(saleVisit);
        return "delete";
    }

    public String edit(){
        saleVisit = saleVisitService.findById(saleVisit.getVisit_id());
        /*System.out.println(saleVisit);*/
        ActionContext.getContext().getValueStack().push(saleVisit);
        return "edit";
    }

    public String update(){
        saleVisitService.update(saleVisit);
        return "update";
    }

}
